﻿using DataAcquisition;
using DataAcquisition.CustomControls;
using gl_SMT.Model;
using gl_SMT.View;
using Microsoft.Office.Interop.Excel;
using SMTUploadProductionData.model;
using System;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace gl_SMT
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
            EventHub.TabRequested += OnTabRequested; ;
        }

        List<string> list_DvName = new List<string>();//设备名称
        List<DeviceType> lsit_devicetype = new List<DeviceType>();//机台类型自定义控件
        DatabaseReadWrite? Database ;
        string str_ConnectData;
        
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Database = new DatabaseReadWrite(ref str_ConnectData);//连接数据库
            if (str_ConnectData != null)
            {
                MessageBox.Show("无法连接数据库");//连接失败
                return;
            }
            AddCustomControls();// 添加自定义控件
            DataAdd();//数据添加
        }

        /// <summary>
        /// 添加自定义控件
        /// </summary>
        private void AddCustomControls()
        {
            DeviceTypeName device = new DeviceTypeName();
            list_DvName.Add(device.gkg_Printing);
            list_DvName.Add(device.hl_Printing);
            list_DvName.Add(device.ymh_Mounter);
            list_DvName.Add(device.juki_Patch);
            for (int i = 0; i < list_DvName.Count; i++)
            {
                DeviceType deviceType = new DeviceType();//添加自定义控件
                deviceType.DeviceName = list_DvName[i];//设备名称
                deviceType.DeviceNumber = GetDNumber(list_DvName[i]).ToString();//在线的数量
                deviceType.closeMainWindow = TabConrolAdd;//绑定委托事件
                deviceType.Margin = new Thickness(0, 20, 0, 0);//自定义控件的间距
                wrapPanel_Device.Children.Add(deviceType);//添加到wrapPanel
                lsit_devicetype.Add(deviceType);//添加到List<DeviceType>方便后续的数据更新
            }
        }

        /// <summary>
        /// 添加机台类型选项卡
        /// </summary>
        public void TabConrolAdd()
        {
            HideTabItem();
            TabItem tab_conrol = new TabItem();//初始化一个选项卡
            tab_conrol.Width = 150;//选项卡的表头宽度
            tab_conrol.Height = 30;//选项卡的表头高度
            tab_conrol.Style = FindResource("ClosableTabItemStyle") as System.Windows.Style;//绑定样式
            tab_conrol.Header = VariableStorage.SelectType;//表头名称
            UserControlMachine Machine = new UserControlMachine();
            tab_conrol.Content = Machine;//绑定自定义控件

            bool isDuplicate = false;
            for (int i = 0; i < tabControl_Main.Items.Count; i++)//遍历全部选项卡
            {
                var existingTabItem = tabControl_Main.Items[i] as TabItem;
                if (existingTabItem != null && tab_conrol.Header != null && existingTabItem.Header.ToString() == tab_conrol.Header.ToString())//判断是否已经添加过选项卡 
                {
                    isDuplicate = true;
                    tabControl_Main.SelectedItem = existingTabItem;
                    break;
                }
            }
            if (isDuplicate ==false) //如果没有添加过该名称的选项卡就添加否则就不添加
            {
                tabControl_Main.Items.Add(tab_conrol);//添加选项卡
                tabControl_Main.SelectedItem = tab_conrol;//索引到添加的选项卡
            }
        }

        /// <summary>
        /// 添加机台数据查询选项卡
        /// </summary>
        /// <param name="tabHeader"></param>
        private void OnTabRequested(string tabHeader)
        {
            HideTabItem();
            TabItem tab_conrol = new TabItem();
            tab_conrol.Width = 170;
            tab_conrol.Height = 30;
            tab_conrol.Style = FindResource("ClosableTabItemStyle") as System.Windows.Style;
            tab_conrol.Header = tabHeader;
            string? HeaderName = tab_conrol.Header.ToString();//表头名称
            if (HeaderName.Contains("GKG自动印刷机"))
            {
                gkg_DataSelect gkg_Control = new gkg_DataSelect();
                gkg_Control.MachineName = tabHeader;
                tab_conrol.Content = gkg_Control;
            }
            else if (HeaderName.Contains("回流焊接机"))
            {
                hl_DataSelect hl_Control = new hl_DataSelect();
                hl_Control.MachineName = tabHeader;
                tab_conrol.Content = hl_Control;
            }
            else if (HeaderName.Contains("雅马哈贴片机"))
            {
                ymh_DataSelect ymh_dataSelect = new ymh_DataSelect();
                ymh_dataSelect.MachineName = tabHeader;
                tab_conrol.Content = ymh_dataSelect;
            }
            else if (HeaderName.Contains("JUKI贴片机"))
            {
                juki_DataSelect juki_Dataselect = new juki_DataSelect();
                juki_Dataselect.MachineName = tabHeader;
                tab_conrol.Content = juki_Dataselect;
            }
            bool isDuplicate = false;
            for (int i = 0; i < tabControl_Main.Items.Count; i++)
            {
                var existingTabItem = tabControl_Main.Items[i] as TabItem;
                if (existingTabItem != null && tab_conrol.Header != null && existingTabItem.Header.ToString() == tab_conrol.Header.ToString())
                {
                    isDuplicate = true;
                    tabControl_Main.SelectedItem = existingTabItem;
                    break;
                }
            }
            if (isDuplicate == false)
            {
                tabControl_Main.Items.Add(tab_conrol);
                tabControl_Main.SelectedItem = tab_conrol;
            }
        }


        private void HideTabItem()
        {
            if (tabControl_Main.Items.Count>7)
            {
                Dispatcher.BeginInvoke(() => tabControl_Main.Items.RemoveAt(1));
                Dispatcher.BeginInvoke(() => tabControl_Main.Items.RemoveAt(2));
            }
        }

        public int GetDNumber(string Name)
        {
            int Number = 0;
            switch (Name)
            {
                case "GKG自动印刷机":
                    return Number = 12;
                case "点精点胶机":
                    return Number = 1;
                case "除湿房":
                    return Number = 6;
                case "富士贴片机":
                    return Number = 6;
                case "回流焊接机":
                    return Number = 3;
                case "丝印印刷机":
                    return Number = 1;
                case "雅马哈贴片机":
                    return Number = 6;
                case "JUKI贴片机":
                    return Number = 2;
                default:
                    return Number;
            }
        }
        
        private void DataAdd()
        {
            Task.Run(() => gkg_DataUpdate(lsit_devicetype[0]));
            Task.Run(() => hl_DataUpdate(lsit_devicetype[1]));
            Task.Run(() => juki_DataUpdate(lsit_devicetype[3]));
            Task.Run(() => ymh_DataUpdate(lsit_devicetype[2]));
        }

        #region  GKG印刷机
        /// <summary>
        /// gkg数据更新
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        private async Task gkg_DataUpdate(DeviceType device)
        {
            while (true)
            {
                //查询数据
                string query = "select * from gkg_Producedate WHERE CAST(TimeStamp AS DATE)=CAST(GETDATE() AS DATE)";
                List<gkg_model> list_gkg = await Database.QueryDataAsync<gkg_model>(query);
                //查询在线数量
                List<string> machineNames = await Database.SelectDataAsync("gkg_Producedate", "TimeStamp");
                string TotalOutput = gkg_Total(list_gkg, machineNames);
                string CycleTime = gkg_CycleTime(list_gkg, machineNames);
                double UPH = await gkg_UPH(list_gkg, machineNames, CycleTime);
                Dispatcher.Invoke(() =>
                {
                    device.txt_ProductionNumber.Text = machineNames.Count.ToString();
                    device.txt_AlmNumber.Text = "0";
                    device.txt_OutPut.Text = TotalOutput.ToString();
                    device.txt_Cycle.Text = CycleTime;
                    device.txt_UPH.Text = UPH.ToString("0.0");
                });
                await Task.Delay(5000);
            }
            
        }

        private string gkg_Total(List<gkg_model> list_gkg,List<string> machineNames)
        {
            int Total = 0;
            foreach (var machineName in machineNames)
            {
                List<gkg_model> gkg = list_gkg.Where(t=>t.MachineName== machineName).ToList();
                Total += gkg[gkg.Count - 1].PCBNumber;
            }
            return Total.ToString();
        }

        /// <summary>
        /// gkg生产时间
        /// </summary>
        /// <returns></returns>
        private string gkg_CycleTime(List<gkg_model> list_gkg, List<string> machineNames)
        {
            TimeSpan timeSpan = TimeSpan.FromSeconds(0);
            for (int i = 0; i < machineNames.Count; i++)
            {
                List<gkg_model> gkg = list_gkg.Where(t=>t.MachineName== machineNames[i]).ToList();
                foreach (var g in gkg)
                {
                    timeSpan += TimeSpan.FromSeconds(int.Parse(g.CycleTime));
                }
            }
            int totalHours = (int)timeSpan.TotalHours;
            string formattedTime = $"{totalHours:D2}:{timeSpan.Minutes:D2}:{timeSpan.Seconds:D2}";
            return formattedTime.ToString();
        }

        /// <summary>
        /// 获取所有机台的单个的生产周期
        /// </summary>
        /// <returns></returns>
        private async Task<double> gkg_UPH(List<gkg_model> list_gkg, List<string> MachineName,string runTime)
        {
            double dou_UPH = 0;
            for (int i = 0; i < MachineName.Count; i++)
            {
                List<gkg_model> gkg = list_gkg.Where(t => t.MachineName == MachineName[i]).ToList();
                if (gkg.Count == 0) continue;
                dou_UPH += gkg[gkg.Count - 1].PCBNumber;
            }
            return dou_UPH / ConvertTimeToSeconds(runTime);
        }

        static double ConvertTimeToSeconds(string timeStr)
        {
            // 按 ":" 分割字符串
            string[] parts = timeStr.Split(':');
            if (parts.Length != 3)
            {
                throw new ArgumentException("时间格式不正确，应为 '时:分:秒'");
            }

            // 转换为整数（小时、分钟、秒）
            if (!double.TryParse(parts[0], out double hours) ||
                !double.TryParse(parts[1], out double minutes) ||
                !double.TryParse(parts[2], out double seconds))
            {
                throw new ArgumentException("时间部分包含非数字字符");
            }

            // 验证时间有效性（可选，根据需求判断是否允许小时超过24）
            if (minutes < 0 || minutes >= 60 || seconds < 0 || seconds >= 60)
            {
                throw new ArgumentException("分钟或秒数超出有效范围（0-59）");
            }

            // 计算总秒数
            return hours + minutes / 60 + seconds/3600;
        }
        #endregion

        #region  回流焊接
        private async Task hl_DataUpdate(DeviceType device)
        {
            while (true)
            {
                string query = "select * from hl_Producedate WHERE CAST(logTime AS DATE)=CAST(GETDATE() AS DATE)";
                List<hl_model> list_hl = await Database.QueryDataAsync<hl_model>(query);
                List<string> machineNames = await Database.SelectDataAsync("hl_Producedate", "logTime");//查询在线数量
                Dispatcher.Invoke(() =>
                {
                    device.txt_ProductionNumber.Text = machineNames.Count.ToString();//在线数量
                    device.txt_AlmNumber.Text = "无";
                    device.txt_OutPut.Text = "无";
                    device.txt_Cycle.Text = "无";
                    device.txt_UPH.Text = "无";
                });
                await Task.Delay(5000);
            }
        }
        #endregion

        #region 雅马哈

        private async Task ymh_DataUpdate(DeviceType device)
        {
            while (true)
            {
                string query = "select * from ymh_Producedate where CAST(Time as date)=CAST(GETDATE() as date)";
                //查询数据
                List<ymh_model> list_gkg = await Database?.QueryDataAsync<ymh_model>(query);
                //查询在线数量
                List<string> machineNames = await Database.SelectDataAsync("ymh_Producedate", "Time");

                string total = await ymh_Total(list_gkg, machineNames);
                string NumberBoards = await ymh_NumberBoards(list_gkg, machineNames);
                string CycleTime = await ymh_ProductionTime(list_gkg, machineNames);
                string UPH = (Convert.ToDouble(total) / ConvertTimeToSeconds(CycleTime)).ToString("0.0") ;
                Dispatcher.Invoke(() =>
                {
                    device.txt_ProductionNumber.Text = machineNames.Count.ToString();//在线数量
                    device.tb_Expected.Text = "总点数";
                    device.txt_AlmNumber.Text = total;
                    device.tb_Production.Text = "总板数";
                    device.txt_OutPut.Text = NumberBoards;
                    device.txt_Cycle.Text = CycleTime;
                    device.txt_UPH.Text = UPH;
                });
                await Task.Delay(5000);
            }
        }

        private async Task<string> ymh_Total(List<ymh_model> list_gkg,List<string> machineNames)
        {
            int list_Total = 0;
            int TotalQuantity = 0;
            for (int i = 0; i < machineNames.Count; i++)
            {
                List<ymh_model> list_ymh = list_gkg.Where(t=>t.MachineName== machineNames[i]).ToList();
                TotalQuantity = 0;
                for (int j = 1; j < list_ymh.Count; j++)
                {
                    try
                    {
                        if (list_ymh[j].TotalQuantity == "") continue;
                        if (list_ymh[j].TotalQuantity == "0") TotalQuantity += int.Parse(list_ymh[j - 1].TotalQuantity);
                        if (j == list_ymh.Count - 1) TotalQuantity += int.Parse(list_ymh[j].TotalQuantity);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        continue;
                    }
                }
                list_Total+=TotalQuantity;
            }
            return list_Total.ToString();
        }


        private async Task<string> ymh_NumberBoards(List<ymh_model> list_ymh, List<string> machineNames)
        {
            int list_Total = 0;
            int TotalQuantity = 0;
            for (int i = 0; i < machineNames.Count; i++)
            {
                List<ymh_model> ymh = list_ymh.Where(t => t.MachineName == machineNames[i]).ToList();
                TotalQuantity = 0;
                for (int j = 1; j < ymh.Count; j++)
                {
                    try
                    {
                        if (ymh[j].TotalQuantity == "") continue;
                        if (ymh[j].TotalQuantity == "0") TotalQuantity += ymh[j - 1].NumberBoards;
                        if (j == ymh.Count - 1) TotalQuantity += ymh[j - 1].NumberBoards;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        continue;
                    }
                }
                list_Total += TotalQuantity;
            }
            return list_Total.ToString();
        }

        /// <summary>
        /// 平均生产周期
        /// </summary>
        /// <returns></returns>
        private async Task<string> ymh_ProductionTime(List<ymh_model> list_ymh, List<string> machineNames)
        {
            TimeSpan[] time=new TimeSpan[6];
            TimeSpan timeSpan = TimeSpan.Parse("0");
            for (int i = 0; i < machineNames.Count; i++)
            {
                timeSpan = TimeSpan.Parse("0");//初始化一个时间
                for (int j = 1; j < list_ymh.Count-1; j++)
                {
                    try
                    {
                        if (list_ymh[j].RunningStatus != "运行中") continue;
                        if (list_ymh[j].Time == list_ymh[j - 1].Time) continue;
                        timeSpan += TimeSpan.Parse(list_ymh[j + 1].Time.Split(' ')[1]) - TimeSpan.Parse(list_ymh[j].Time.Split(' ')[1]);//将所有的生产周期进行相加
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }
                }
                time[i] = timeSpan;
            }
            timeSpan = TimeSpan.Parse("0");
            for (int j = 0; j < time.Length; j++)
            {
                timeSpan += time[j];
            }
            int totalHours = (int)timeSpan.TotalHours;
            string formattedTime = $"{totalHours:D2}:{timeSpan.Minutes:D2}:{timeSpan.Seconds:D2}";
            return formattedTime;
        }

        #endregion

        #region  JUKI贴片机
        private async Task juki_DataUpdate(DeviceType device)
        {
            while (true)
            {
                List<juki_model> list_juki = new List<juki_model>();
                await Database.SelectDataAsync(list_juki);
                List<string> machineNames = await Database.SelectDataAsync("juki_Producedate", "StartTime");//查询在线数量
                int TotaNumber = await juki_Production(list_juki);//生产总数
                int TotalPlace = await juki_Qualified(list_juki);//良品总数
                string RepairTime = await juki_CycleTime(list_juki);//运行时间
                string UPH = (TotalPlace / ConvertTimeToSeconds(RepairTime)).ToString("0.0");
                Dispatcher.Invoke(() =>
                {
                    device.txt_ProductionNumber.Text = machineNames.Count.ToString();//在线数量
                    device.tb_Production.Text = "良品数量";
                    device.txt_AlmNumber.Text = TotaNumber.ToString();//总产量
                    device.tb_Expected.Text = "生产总数";
                    device.txt_OutPut.Text = TotalPlace.ToString();//良品总数
                    device.txt_Cycle.Text = RepairTime.ToString();//平均生产时间
                    device.txt_UPH.Text = UPH;
                });
                await Task.Delay(5000);
            }
        }

        /// <summary>
        /// 生产总数
        /// </summary>
        /// <returns></returns>
        private Task<int> juki_Production(List<juki_model> juki_Models)
        {
            int TotalNumber = 0;
            foreach (var juki in juki_Models)
            {
                TotalNumber += juki.TotalPick;
            }
            return Task.FromResult(TotalNumber);
        }

        /// <summary>
        /// 良品总数
        /// </summary>
        /// <returns></returns>
        private Task<int> juki_Qualified(List<juki_model> juki_Models)
        {
            int TotalPlace = 0;
            foreach (var juki in juki_Models)
            {
                TotalPlace += juki.TotalPlace;
            }
            return Task.FromResult(TotalPlace);
        }

        private Task<string> juki_CycleTime(List<juki_model> juki_Models)
        {
            TimeSpan CycleTime=TimeSpan.Parse("0");
            foreach (var juki in juki_Models)
            {
                CycleTime += TimeSpan.Parse(juki.RunningTime);
            }
            return Task.FromResult<string>((CycleTime).ToString());
        }

        private Task<string> juki_UPH(int TNumber,string RTime,int OnlineQuantity)
        {
            double Htime = double.Parse(RTime.Split(':')[0]);
            double Mtime = double.Parse(RTime.Split(':')[1]);
            double totalTime = Htime + Mtime / 60;
            string Number = (TNumber / totalTime/ OnlineQuantity).ToString("0.0");
            return Task.FromResult(Number);
        }

        #endregion

        
    }
}